﻿#-*- coding:utf-8 -*-
from django.shortcuts import render_to_response
from django.http import HttpResponse,HttpResponseRedirect
import simplejson,sys,os,urllib,re,hashlib,md5,random,datetime
from dict2xml import dict2xml
from zz91page import *
reload(sys)
sys.setdefaultencoding('UTF-8')
nowpath=os.path.dirname(__file__)
execfile(nowpath+"/func/erm_db.py")
db=ermdb()

def login(request):
    return render_to_response('login.html',locals())
def logincheck(request):
    username=request.POST.get('username')
    password=passwd=request.POST.get('password')
    if passwd:
        md5pwd = hashlib.md5(passwd)
        md5pwd = md5pwd.hexdigest()[8:-8]
    
    if username and password:
        sql='select id,username,password from user where username=%s and password=%s'
        result=db.fetchonedb(sql,[username,md5pwd])
        if result:
            user_id=result[0]
            request.session.set_expiry(6000*60000)
            request.session['username']=result[1]
            request.session['password']=result[2]
            return HttpResponseRedirect('main.html')
        else:
            errtext="用户名或密码错误"
            return render_to_response('login.html',locals())
    else:
        errtext="请输入用户名或密码"
        return render_to_response('login.html',locals())
def token_login(request):
    usertoken=request.GET.get("token")
    company_id=request.GET.get("company_id")
    selfid=request.GET.get("selfid")
    xmltype = request.GET.get('xmltype')
    if selfid:
        #过期时间为2小时  MINUTE,HOUR
        sql="select token from app_token where selfid=%s and token=%s and TIMESTAMPDIFF(HOUR,gmt_modified,NOW())<=1"
        listd=db.fetchonedb(sql,[selfid,usertoken])
        if listd:
            sql="select id,group_id,selfid,company_id,utype,username from users where selfid=%s"
            result=db.fetchonedb(sql,[selfid])
            if result:
                user_id=result['id']
                group_id=result['group_id']
                user_selfid=result['selfid']
                company_id=result['company_id']
                utype=result['utype']
                username=result['username']
                request.session.set_expiry(6000*60000)
                request.session['user_id']=user_id
                request.session['username']=username
                request.session['group_id']=group_id
                request.session['user_selfid']=user_selfid
                request.session['company_id']=company_id
                request.session['utype']=utype
                return HttpResponseRedirect('/main.html')
            else:
                return HttpResponseRedirect('/login.html')
        else:
            return HttpResponseRedirect('/login.html')
    else:
        return HttpResponseRedirect('/login.html')
def logout(request):
    try:
        request.session.delete()
        del request.session['username']
        del request.session['user_id']
        del request.session['group_id']
        del request.session['user_selfid']
        del request.session['company_id']
        del request.session['utype']
        
    except KeyError:
        pass
    return HttpResponseRedirect("login.html")
